import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Index from '../views/Index.vue'
import Login from '../views/Login.vue'
import store from'../store/index'
Vue.use(VueRouter)

const routes = [{
        path: '/Home',
        name: 'Home',
        component: Home,
        meta: {
            auth: true,
            roles:['manager','staff']
        }
    },
    {
        path: '/',
        name: 'Index',
        component: Index,
        meta: {
            auth: true,
            roles:['manager','staff','ordinary']
        }
    },
    {
        path: '/Login',
        name: 'Login',
        component: Login
    },
    {
        path: '/about',
        name: 'About',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/About.vue'),
        meta: {
            auth: true,
            roles:['manager']
        }

    }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (to.meta.auth) {
        if (store.state.userStore.user.name) {
            console.log(store.state.userStore.user.type)
            if (to.meta.roles.indexOf(store.state.userStore.user.type)>=0) {
                next()
            }else {
                console.log('你没有登录权限')
                console.log(from)
                next(from.path)

            }
            
        } else {
            // router.push('/Login')
            next({path:'/login'})
        }
    } else {
        // 不需要验证就可以进入的页面
        next()
    }
})
export default router